package com.jdew.thkj.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.adapter.ProductAdapter;
import com.jdew.thkj.adapter.ProductAdapter2;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.bean.ZhaoshangBean;
import com.jdew.thkj.dialog.CommonDialog;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.manager.WrapContentLinearLayoutManager;
import com.jdew.thkj.onitemInterface.OnItemClickListener;
import com.jdew.thkj.utils.RegexValidateUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by yangyang on 2017/9/11.
 */

public class BiTuiActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.tvRefresh)
    TextView tvRefresh;
    @Bind(R.id.swipe_target)
    RecyclerView recycleView;
    @Bind(R.id.progress)
    ProgressBar progress;
    @Bind(R.id.tv_load_prompt)
    TextView tvLoadPrompt;
    @Bind(R.id.netclick)
    TextView netclick;
    @Bind(R.id.nonetwork)
    LinearLayout nonetwork;
    @Bind(R.id.swipelayout)
    LinearLayout swipelayout;
    @Bind(R.id.pailie2)
    ImageView pailie2;
    @Bind(R.id.pailie1)
    ImageView pailie1;
    @Bind(R.id.swipe_load_more_footer)
    LinearLayout swipeLoadMoreFooter;
    @Bind(R.id.arror_top)
    ImageView arrorTop;
    private ProductAdapter adapter;
    private ProductAdapter2 adapter2;
    private int page = 1;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private String token;
    private String mPid;
    private String ItemId;
    private Dialog mWeiboDialog;
    private List<ZhaoshangBean.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean> list = new ArrayList<>();

    @Override
    public int bindLayout() {
        return R.layout.activity_live;
    }

    @Override
    public void initView(View view) {
        adapter = new ProductAdapter(this, list);
        adapter2 = new ProductAdapter2(this, list);
        back.setVisibility(View.VISIBLE);
        title.setText("每周必推");
        view.findViewById(R.id.back).setOnClickListener(this);
        pailie1.setVisibility(View.GONE);
        pailie2.setVisibility(View.VISIBLE);
        arrorTop.setOnClickListener(this);
        pailie1.setOnClickListener(this);
        pailie2.setOnClickListener(this);
        recycleView.setLayoutManager(new StaggeredGridLayoutManager(2, WrapContentLinearLayoutManager.VERTICAL));
        recycleView.setAdapter(adapter2);
//        swipeToLoadLayout.setOnLoadMoreListener(this);
//        swipeToLoadLayout.setOnRefreshListener(this);
        netclick.setOnClickListener(this);
        adapter.setItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (!RegexValidateUtils.isFastClick()) {
                    return;
                }
                if (sp.getString("SpareTwo", "").equals("")) {
                    startActivity(new Intent(BiTuiActivity.this, LoginActivity.class));
                } else if (sp.getString("UserSjPhone", "").equals("") && (!sp.getString("SpareTwo", "").equals("1"))) {
                    toset("检测到您还没有绑定上级,请到我的页面绑定上级");
                } else if (token.equals("")) {
                    toset();
                } else if (mPid.equals(AppConfig.MPID)) {
                    toAdu();
                } else {
                    ItemId = list.get(position).getNum_iid();
                    toDetail();
                }
            }
        });
        adapter2.setItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (!RegexValidateUtils.isFastClick()) {
                    return;
                }
                if (sp.getString("SpareTwo", "").equals("")) {
                    startActivity(new Intent(BiTuiActivity.this, LoginActivity.class));
                } else if (sp.getString("UserSjPhone", "").equals("") && (!sp.getString("SpareTwo", "").equals("1"))) {
                    toset("检测到您还没有绑定上级,请到我的页面绑定上级");
                } else if (token.equals("")) {
                    toset();
                } else if (mPid.equals(AppConfig.MPID)) {
                    toAdu();
                } else {
                    ItemId = list.get(position).getNum_iid();
                    toDetail();
                }
            }
        });
//        superSearch();
        GoTopView(recycleView, arrorTop);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            case R.id.netclick: {
//                onRefresh();
            }
            break;
            case R.id.pailie1: {
                pailie1.setVisibility(View.GONE);
                pailie2.setVisibility(View.VISIBLE);
                recycleView.setLayoutManager(new StaggeredGridLayoutManager(2, WrapContentLinearLayoutManager.VERTICAL));
                recycleView.setAdapter(adapter2);

            }
            break;
            case R.id.pailie2: {
                pailie1.setVisibility(View.VISIBLE);
                pailie2.setVisibility(View.GONE);
                setRcycleview(recycleView, adapter);
            }
            break;
            case R.id.arror_top: {
                recycleView.smoothScrollToPosition(0);
                arrorTop.setVisibility(View.GONE);
            }
            break;
            default:
                break;
        }
    }

//    @Override
//    public void onLoadMore() {
//        page = page + 1;
//        superSearch();
//        swipeToLoadLayout.setLoadingMore(false);
//    }
//
//    @Override
//    public void onRefresh() {
//        page = 1;
//        superSearch();
//        swipeToLoadLayout.setRefreshing(false);
//    }

    @Override
    protected void onResume() {
        super.onResume();
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        token = sp.getString("token", "");
        mPid = sp.getString("mpid", "");
    }

//    public void superSearch() {
//        if (!DeviceUtils.isNetworkAvalible(BiTuiActivity.this)) {
//            swipelayout.setVisibility(View.GONE);
//            nonetwork.setVisibility(View.VISIBLE);
//            ToastUtils.ToastMessage(BiTuiActivity.this, "网络连接错误请检查");
//            return;
//        } else {
//            swipelayout.setVisibility(View.VISIBLE);
//            nonetwork.setVisibility(View.GONE);
//        }
//        mWeiboDialog = WeiboDialogUtils.createLoadingDialog(this);
//        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).GetWillpush
//                (page, MD5Utils.getMD5("S"+sp.getString("seceret","")+"PageIndex"+page+sp.getString("seceret","")+"S").toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<ZhaoshangBean>() {
//            @Override
//            public void onCompleted() {
//               WeiboDialogUtils.closeDialog(mWeiboDialog);
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                WeiboDialogUtils.closeDialog(mWeiboDialog);
//            }
//
//            @Override
//            public void onNext(ZhaoshangBean bean) {
//                WeiboDialogUtils.closeDialog(mWeiboDialog);
//                if (bean.isSuccess()) {
//                    if (page == 1) {
//                        list.clear();
//                    }
//                    list.addAll(bean.getData().getTbk_uatm_favorites_item_get_response().getResults().getUatm_tbk_item());
//                    if (bean.getData().getTbk_uatm_favorites_item_get_response().getResults().getUatm_tbk_item().size() < 20 && bean.getData().getTbk_uatm_favorites_item_get_response().getResults().getUatm_tbk_item().size() > 0) {
//                        tvLoadPrompt.setText("无更多数据");
//                        swipeToLoadLayout.setLoadingMore(false);
//                    } else if (bean.getData().getTbk_uatm_favorites_item_get_response().getResults().getUatm_tbk_item().size() >= 20) {
//                        tvLoadPrompt.setText("正在加载");
//                    } else {
//                        tvLoadPrompt.setText("无更多数据");
//                        swipeToLoadLayout.setLoadingMore(false);
//                    }
//                    adapter.setDataList(list);
//                    adapter2.setDataList(list);
//                }
//            }
//        });
//
//    }

    public void toset() {
        new CommonDialog.Builder(this)
                .setTitle("提示")
                .setMessage("授权失效,请联系客服")
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    }
                }).show();
    }

    public void toDetail() {
        Bundle b = new Bundle();
        b.putSerializable("itemId", ItemId);
        startActivity(TbProductDetailActivity.class, b);
    }

    public void toAdu() {
        new CommonDialog.Builder(this)
                .setTitle("提示")
                .setMessage("设置默认推广位")
                .setPositiveButton("设置", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent in = new Intent(BiTuiActivity.this, WebViewActivity.class);
                        in.putExtra("type", 2);
                        in.putExtra("url", "http://pub.alimama.com/");
                        startActivity(in);
                    }
                }).show();
    }

    public void GoTopView(RecyclerView recycleview, final ImageView gotop) {
        recycleview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            int mScrollThreshold;

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                boolean isSignificantDelta = Math.abs(dy) > mScrollThreshold;
                if (isSignificantDelta) {
                    if (dy > 0) {
                        gotop.setVisibility(View.VISIBLE);
                    } else {
                        if (!recyclerView.canScrollVertically(-1)) {
                            gotop.setVisibility(View.GONE);
                        }
                    }
                }
            }
        });
    }
}
